/**
 * 
 */
package com.hoopchina.ui.search;

import java.util.LinkedList;
import java.util.List;

import com.hoopchina.news.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;


/**
 * SearchJsonListAdapter
 * @author Guo Baiyu
 *
 */
public class SearchJsonListAdapter extends BaseAdapter {

    
	private List<String>	            mDataList;
	private Context	                    mContext;
	private View	                    mCloseItemView;
	private Button	                    mCloseButton;
	private SugCloseButtonCloseObserver	mObserver;


	/**
     * @param mObserver the mObserver to set
     */
    public void setSugCloseButtonCloseObserver(SugCloseButtonCloseObserver observer) {
    	this.mObserver = observer;
    }

	/**
     * 
     * @param context
     */
    public SearchJsonListAdapter( Context context ) {
        super();
//        mList = new LinkedList<TextListItem>();
        mDataList  = new  LinkedList<String>();
        mContext = context;
        
		LayoutInflater inflater = LayoutInflater.from(mContext);

		mCloseItemView = inflater.inflate(R.layout.searchsuglistclose, null);
		mCloseButton = (Button) mCloseItemView.findViewById(R.id.buttonCloseJsonList);
		mCloseButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mObserver.onClickSugColseButtonListener();
			}
		});
    }

    /**
	 * 
	 */
    @Override
    public int getCount() {
        return mDataList.size() + 1;
    }

    /**
	 * 
	 */
    @Override
    public Object getItem( int position ) {
        return mDataList.get( position );
    }

    /**
	 * 
	 */
    @Override
    public long getItemId( int position ) {
        return position;
    }

    /**
	 * 
	 */
    @Override
    public View getView( int position, View convertView, ViewGroup parent ) {
    	if (position >= mDataList.size()) {
    		return mCloseItemView;
    	}
    	SearchJsonListTextItem itemView = null;
    	if (convertView == null || convertView == mCloseItemView) {
    		itemView = new SearchJsonListTextItem(mContext);
    	}
    	else {
    		itemView = (SearchJsonListTextItem)convertView;
    	}

    	itemView.setItemText( mDataList.get( position ) );
        return itemView;
    }

    /**
     * 
     * @param itemView
     */
    public void addItem( String itemView ) {
    	mDataList.add( itemView );

        this.notifyDataSetChanged();
    }
    /**
     * 
     * @param itemView
     */
    public void addAllItem( List<String> dataList) {
    	mDataList.addAll( dataList );
        this.notifyDataSetChanged();
    }
    
//    public void  addFooterViewForClearButton() {
//  		LayoutInflater inflater = 
//  				( LayoutInflater )getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
//  			inflater.inflate( R.layout.searchsuglistclose, this, true );
//  			
//  			LinearLayout layout = ( LinearLayout )findViewById( R.id.linearLayoutSearchSugClose );
//  			
//  			Button closeButton = ( Button ) layout.findViewById( R.id.buttonCloseJsonList );
//  			closeButton.setOnClickListener( new OnClickListener() {
//              @Override
//              public void onClick( View v ) {
//                  // TODO Auto-generated method stub
//                  setJosnViewVisibility( SearchJsonList.GONE );
//              }
//          });
//          mSearchJsonListView.addFooterView(layout);
//      }
}
